package com.itcast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.health.constant.MessageConstant;
import com.itcast.health.pojo.CheckGroup;
import com.itcast.health.pojo.PageResult;
import com.itcast.health.pojo.QueryPageBean;
import com.itcast.health.pojo.Result;
import com.itcast.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Gao
 * @version v1.0
 * @date 2019/11/18 12:57
 * @description
 **/
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService ckeckGroupService;

    /**
     * 在addCheckGroup方法中完成增加检查组和对应信息添加
     *
     * @param checkGroup   检查组信息
     * @param checkitemIds 检查组对应的检查项id集合
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {

        try {
            ckeckGroupService.addCheckGroup(checkGroup, checkitemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }


    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = ckeckGroupService.findPage(queryPageBean);
        return pageResult;
    }


    /**
     * 根据id 查询检查组信息
     *
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public Result findById(Integer id) {
        CheckGroup checkGroup = null;
        try {
            checkGroup = ckeckGroupService.findCkeckGroupById(id);
        } catch (Exception e) {
            return new Result(false, "获取数据失败,请刷新页面");
        }
        return new Result(true, "", checkGroup);
    }

    /**
     * 根据id查询检查组所包含的检查项id
     *
     * @param id
     * @return
     */
    @RequestMapping("findcheckitemIdsByCheckGroupId")
    public List<Integer> findcheckitemIdsByCheckGroupId(Integer id) {
        return ckeckGroupService.findcheckitemIdsByCheckGroupId(id);
    }

    /**
     * 重新编辑检查组信息和对应的检查项
     *
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @RequestMapping("edit")
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            ckeckGroupService.editckeckGroup(checkGroup, checkitemIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 根据id删除对应的检查组信息
     * @param checkGroupid
     * @return
     */
    @RequestMapping("deleteById")
    public Result deleteById(Integer checkGroupid) {
        try {
            ckeckGroupService.deleteCheckGroupById(checkGroupid);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }
}